﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DataUpload;

namespace logistics
{
    public partial class SystemLogs : Form
    {
        public SystemLogs()
        {
            InitializeComponent();
            InitializeControl();
            InitializeData();
            lvwSystemLog.Visible = false;
        }

        private void InitializeData()
        {
            InitaializeList();
        }

        private void InitializeControl()
        {
            this.dateTime.Format = DevComponents.Editors.eDateTimePickerFormat.Custom;
            this.dateTime.CustomFormat = "yyyy-MM-dd ";
            this.dateTime.Value = DateTime.Now;
            WL.BLL.Users bll = new WL.BLL.Users();
            DataSet ds = bll.GetAllList();
            if (ds.Tables.Count > 0)
            {
                DataHelp.InitializeControl(cmbUser, ds.Tables[0], "Name", "UserName", true);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            InitaializeList();
        }

        private void InitaializeList()
        {
            string UserName = cmbUser.SelectedValue.ToString();
            string data1 = dateTime.Value.ToString("yyyy-MM-dd 00:00:00");
            string data2 = dateTime.Value.ToString("yyyy-MM-dd 23:59:59");
            String strSql = string.Empty;
            strSql = "Createdtm between '" + data1 + "'and  '" + data2 + "'";         

            if (lvwSystemLog.Visible)
            {
                if (UserName != "-1")
                {
                    strSql += " and  UserName='" + UserName + "'";
                }
                selectSystemLog(strSql);
            }
            else
            {
                if (UserName != "-1")
                {
                    strSql += " and  CreateUser='" + UserName + "'";
                }
                SelectData(strSql);
            }
        }

        private void selectSystemLog(string strsql)
        {         
            WL.BLL.SystemLogs bll = new WL.BLL.SystemLogs();
            DataSet ds = bll.GetList(strsql);
            BadingLvw(ds);
        }

        private void SelectData(string strSql)
        {        
            GetLogDataTale(strSql, dgvAdd, "EventName='添加' ", 1);
            GetLogDataTale(strSql, dgvEdit, "EventName='修改' ", 2);
            GetLogDataTale(strSql, dgvDelete, "EventName='删除' ", 3);
        }

        private void GetLogDataTale(string strSql, DataGridView dgvName, string where, int n)
        {
            WL.BLL.WaybillInforLogs bll = new WL.BLL.WaybillInforLogs();
            string str = strSql + " and " + where;
            DataSet ds = bll.GetList(str);
            if (ds.Tables[0].Rows.Count > 0)
            {
                //设定列不能自动作成  
                dgvName.AutoGenerateColumns = false;
                DataTable dt = ds.Tables[0];
                dgvName.RowCount = dt.Rows.Count;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dgvName.Rows[i].Cells["ID" + n.ToString()].Value = i.ToString();
                    dgvName.Rows[i].Cells["WaybillID" + n.ToString()].Value = dt.Rows[i]["WaybillID"].ToString();
                    dgvName.Rows[i].Cells["EventName" + n.ToString()].Value = dt.Rows[i]["EventName"].ToString();
                    dgvName.Rows[i].Cells["LogNote" + n.ToString()].Value = dt.Rows[i]["LogNote"].ToString();
                    dgvName.Rows[i].Cells["Createdtm" + n.ToString()].Value = dt.Rows[i]["Createdtm"].ToString();
                    dgvName.Rows[i].Cells["CreateUser" + n.ToString()].Value = dt.Rows[i]["CreateUser"].ToString();
                    dgvName.Rows[i].Cells["Note" + n.ToString()].Value = dt.Rows[i]["Note"].ToString();

                }
            }
            else
            {
                dgvName.DataSource = null;
                dgvName.Rows.Clear();
            }
        }

        private void btnDealLog_Click(object sender, EventArgs e)
        {
            lvwSystemLog.Visible = false;
            tabControlDealLog.Visible = true;
        }

        private void btnSystemLog_Click(object sender, EventArgs e)
        {
            lvwSystemLog.Visible = true;
            tabControlDealLog.Visible = false;
            lvwSystemLog.Items.Clear();
            lvwSystemLog.Columns.Clear();
            InitaializeList();
        }

        private void BadingLvw(DataSet ds)
        {
            lvwSystemLog.Clear();
            int count = ds.Tables[0].Rows.Count;
            lvwSystemLog.View = View.Details;
            lvwSystemLog.GridLines = true;//显示网格线

            this.lvwSystemLog.Columns.Add("编号", 80, HorizontalAlignment.Center); 
            this.lvwSystemLog.Columns.Add("用户名", 80, HorizontalAlignment.Center);   
            this.lvwSystemLog.Columns.Add("事件名", 80, HorizontalAlignment.Center);    
            this.lvwSystemLog.Columns.Add("内容", 400, HorizontalAlignment.Center);             
            this.lvwSystemLog.Columns.Add("时间", 150, HorizontalAlignment.Center);            
            this.lvwSystemLog.Columns.Add("说明", 200, HorizontalAlignment.Center);                  
            
            if (count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    List<string> list = new List<string>();                   
                    list.Add(row["SystemLogsID"].ToString());
                    list.Add(row["UserName"].ToString());
                    list.Add(row["EventName"].ToString());
                    list.Add(row["LogNote"].ToString());
                    list.Add(row["Createdtm"].ToString());
                    list.Add(row["Note"].ToString());    
                    ListViewItem items = new ListViewItem(list.ToArray(), 0);
                    lvwSystemLog.Items.Add(items);
                }
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((keyData == Keys.Enter))
            {
                InitaializeList();
                return base.ProcessDialogKey(Keys.Tab);
            }
            else
            {
                return base.ProcessDialogKey(keyData);
            }
        }
    }
}


